package com.pmt.feedplayer.platform

import com.pmt.feedplayer.data.PlaybackState
import com.pmt.feedplayer.data.PlaybackProgress
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow

/**
 * JVM平台的Player实现（用于开发和测试环境）
 */
actual class PlayerImpl : Player {
    private val _playbackStateFlow = MutableStateFlow(PlaybackState.IDLE)
    private val _progressFlow = MutableStateFlow(PlaybackProgress(0, 0, 0))
    private val _errorFlow = MutableSharedFlow<String>()
    private var currentUrl: String? = null
    
    override val playbackStateFlow = _playbackStateFlow.asStateFlow()
    override val progressFlow = _progressFlow.asStateFlow()
    override val errorFlow = _errorFlow.asSharedFlow()
    
    override fun setDataSource(url: String) {
        currentUrl = url
        _playbackStateFlow.value = PlaybackState.PREPARING
        // 模拟准备完成
        _playbackStateFlow.value = PlaybackState.PLAYING
        _progressFlow.value = PlaybackProgress(0, 60000, 60000) // 模拟1分钟视频
    }
    
    override fun play() {
        _playbackStateFlow.value = PlaybackState.PLAYING
    }
    
    override fun pause() {
        _playbackStateFlow.value = PlaybackState.PAUSED
    }
    
    override fun seekTo(position: Long) {
        _progressFlow.value = _progressFlow.value.copy(currentPosition = position)
    }
    
    override fun setVolume(volume: Float) {
        // JVM实现不处理实际的音频
    }
    
    override fun release() {
        _playbackStateFlow.value = PlaybackState.IDLE
        currentUrl = null
    }
    
    override fun getPlayerView(): Any? {
        // JVM实现返回null，因为没有实际的视图组件
        return null
    }
}

/**
 * 创建JVM平台的Player实例
 */
actual fun createPlayer(): Player {
    return PlayerImpl()
}